package com.aliyunlog.module.task.tracker;

import java.util.Date;

import com.aliyunlog.model.ScheduleJob;
import com.aliyunlog.module.task.service.JobTaskService;
import com.aliyunlog.utils.LocalCacheUtil;
import com.aliyunlog.utils.SpringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.aliyunlog.query.LogQuery;

/**
 * 日志监控任务跟踪
 * @author linqunxuan
 * @date 2017年6月26日
 * @version 1.0
 */
@Component
public class LogMonitorTracker {

    private static Logger log = Logger.getLogger(LogMonitorTracker.class);

    public void run(String confCode) {
        Date now = new Date();
		if(StringUtils.isBlank(confCode)){
			log.debug("confCode为空,无法执行任务 ");
		}else{
            JobTaskService taskService = SpringUtils.getBean(JobTaskService.class);
            ScheduleJob scheduleJob = taskService.selectJobByConfCode(confCode);
            if(scheduleJob != null){
                Object o = LocalCacheUtil.createLocalCache().get(confCode);
                if(o != null){
                    LogQuery.sendDingDing(confCode, scheduleJob.getQueryKey(), (Date) o);
                }else{
                    LogQuery.sendDingDing(confCode, scheduleJob.getQueryKey(), null);
                }
                LocalCacheUtil.createLocalCache().put(confCode, now);

                log.debug(" run...................................... confCode =" + confCode +" currentDate = "+ (now));
                log.debug(" localCacheUtil cache size =  " + LocalCacheUtil.createLocalCache().size());
            }
		}
		
		
	}
}